package com.starpl.mobile.android.monthunsae.november.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

import com.starpl.mobile.android.monthunsae.november.R;
import com.starpl.mobile.android.monthunsae.november.domain.UserInfo;
import com.starpl.mobile.android.platform.command.StarplCommand;
import com.starpl.mobile.android.platform.common.CameraUtil;
import com.starpl.mobile.android.platform.common.base.BaseActivity;
import com.starpl.mobile.android.platform.common.base.BaseApplication;
import com.starpl.mobile.android.platform.common.image.ImageDownloader;
import com.starpl.mobile.android.platform.common.network.HttpHandler;


/**
 * 유저 프로필 변경을 위한 다이얼로그형 Activity
 * 
 * @author 우연화
 * @since 2011.03.16
 */
public class SettingProfileImageActivity extends BaseActivity implements OnClickListener
{
	private static final int IMAGE_RESIZE_LIMIT = 90;
	private static final String DEFAULT_IMAGE_PATH = "http://shared.starpl.com/images/home/common/img_profile.gif";
	private ImageView mIvProfile;
	private String mProfileImgPath;
	private String mUserId;
	private int mUserIdx;
	private CameraUtil mCamera;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting_profile_image_activity);
		
		mIvProfile = (ImageView)findViewById(R.id.setting_profile_image_activity_iv_profile);
		Button bChangeImage = (Button)findViewById(R.id.setting_profile_image_activity_b_change);
		Button bCancel = (Button)findViewById(R.id.setting_profile_image_activity_b_cancel);
		
		bChangeImage.setOnClickListener(this);
		bCancel.setOnClickListener(this);
		
		setImage();
	}

	private void setImage()
	{
		HttpHandler getuserInfo = new HttpHandler(this, "이미지 가져오는 중")
		{
			@Override
			public void didSucceed(Object result)
			{
				JSONObject object = (JSONObject) result;
				
				JSONObject data;
				try
				{
					// 프로필 이미지 경로를 받아온다.
					data = object.getJSONObject("data");
					String profileImgPath =  data.getString("profileImgPath");
					
					// 이미지를 변경하지 않은 최초이미지이면 사진변경 이미지를 보여준다.
					if(profileImgPath.equals(DEFAULT_IMAGE_PATH))
					{
						mIvProfile.setBackgroundResource(R.drawable.img_picture_change);
					}
					else
					{
						// 프로필 이미지를 이미지 뷰에 세팅한다.
						ImageDownloader.download(profileImgPath, mIvProfile, true);
					}
					
					
				}
				catch(JSONException e)
				{
					LOG.e(e.getMessage());
				}

				// 유저 프로필 정보를 받아온다.
			}
		};
		
		// 유저Id를 받아온다.
		UserInfo userInfo = (UserInfo) BaseApplication.getGlobalPersistent("userInfo");
		this.mUserId = userInfo.getUserId();
		this.mUserIdx = userInfo.getUserIdx();
		
		StarplCommand.getUserInfo(getuserInfo, mUserId);
	}

	/* (non-Javadoc)
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v)
	{
		switch(v.getId())
		{
		// 이미지 변경
		case R.id.setting_profile_image_activity_b_change:
			mCamera = new CameraUtil(this);
			mCamera.openCameraOrAlbumForCrop(mUserIdx, IMAGE_RESIZE_LIMIT);
			break;
			
		// 이미지 변경 취소
		case R.id.setting_profile_image_activity_b_cancel:
			finish();
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if(resultCode == RESULT_OK)
		{
			final HttpHandler httpHandler = new HttpHandler(this, "이미지 변경중...")
			{
				@Override
				public void didSucceed(Object result)
				{
					ImageDownloader.download(mProfileImgPath, mIvProfile, true);
				}
			};
			
			HttpHandler uploadHandler = new HttpHandler(this, "이미지 업로드중...")
			{
				@Override
				public void didSucceed(Object result)
				{
					try
					{
						JSONObject object = new JSONObject(result.toString());
						String hostName = object.getString("hostname");
						String imagePath = object.getString("original");
						mProfileImgPath = "http://" + hostName + imagePath;
						
						StarplCommand.updateProfileImage(httpHandler, mProfileImgPath);
					}
					catch(JSONException e)
					{
						LOG.e(e.getMessage());
					}
				}
			};
			
			mCamera.process(requestCode, data, uploadHandler);
		}
	}
}
